package com.uusee.crawler.pageprocessor.baike.xunlei;

import java.util.Date;

import javax.persistence.Transient;

import org.apache.oro.text.regex.MatchResult;
import org.apache.oro.text.regex.Pattern;
import org.apache.oro.text.regex.PatternCompiler;
import org.apache.oro.text.regex.PatternMatcher;
import org.apache.oro.text.regex.PatternMatcherInput;
import org.apache.oro.text.regex.Perl5Compiler;
import org.apache.oro.text.regex.Perl5Matcher;

import com.uusee.crawler.framework.Processor;
import com.uusee.crawler.model.CrawlStatusCodes;
import com.uusee.crawler.model.CrawlURI;
import com.uusee.crawler.util.PageProcessorUtils;
import com.uusee.shipshape.bk.model.Baike;
import com.uusee.shipshape.bk.model.BaikeDataSource;
import com.uusee.util.StringUtils;

public class XunleiMoviePlotsPageProcessor extends Processor {
	private static final String NAME_REG="<div class=\"mov-detail-box\">\\s+<h2><strong><a href=\"http://data.movie.xunlei.com/movie/\\d+\">([^<]*?)</a></strong><em>[^<]*?</em><span>[^<]*?</span></h2>";
	private static final String EN_NAME_REG="<div class=\"mov-detail-box\">\\s+<h2><strong><a href=\"http://data.movie.xunlei.com/movie/\\d+\">[^<]*?</a></strong><em>([^<]*?)</em><span>[^<]*?</span></h2>";
	private static final String YEAR_REG="<div class=\"mov-detail-box\">\\s+<h2><strong><a href=\"http://data.movie.xunlei.com/movie/\\d+\">[^<]*?</a></strong><em>[^<]*?</em><span>([^<]*?)</span></h2>";
	private static final String LOGO_REG="<div class=\"mov-detail-info\">\\s+<div class=\"mov-pic-flash\">\\s+<div class=\"big-pic\"><a[^<]*?><img[^<]*?src=\"([^<]*?)\"[^<]*?/></a></div>";
	private static final String CHANNEL_CODE_REG="<script>.*?var movie_type = '(.*?)';.*?</script>";
	private static final String SUMMARY_REG="<div class=\"mov-drama.*?\">\\s+<h3>(.*?)</h3>\\s+(\\S.*?\\S)\\s+</div>";
	private static final String SUMMARY_REG1="<div class=\"mov-drama.*?\">\\s+<h3>剧情介绍</h3>\\s+(\\S.*?\\S)\\s+</div>";
	private static final String SUMMARY_REG2="<div class=\"mov-drama.*?\">\\s+<h3>幕后</h3>\\s+(\\S.*?\\S)\\s+</div>";
	private static final String SUMMARY_REG3="<div class=\"mov-drama.*?\">\\s+<h3>花絮</h3>\\s+(\\S.*?\\S)\\s+</div>";
	private static final String SUMMARY_REG4="<div class=\"mov-drama.*?\">\\s+<h3>影片评价</h3>\\s+(\\S.*?\\S)\\s+</div>";
	
						

	protected void innerProcess(CrawlURI crawlUri) {
		if (!canProcess(crawlUri)) {
			return;
		}
		String sourceSite = crawlUri.getSourceSite();
		String crawlUrl = crawlUri.getCrawlUrl();
		String plotsPage = crawlUri.getCrawlResult();
		
		BaikeDataSource baike = (BaikeDataSource)crawlUri.getModel();
		String summary = "";
		try {
			
			PatternCompiler compiler = new Perl5Compiler();
			Pattern pattern = compiler.compile(SUMMARY_REG, Perl5Compiler.CASE_INSENSITIVE_MASK);
			PatternMatcher matcher = new Perl5Matcher();
			PatternMatcherInput input = new PatternMatcherInput(plotsPage);
			StringBuffer sb = new StringBuffer();
			while (matcher.contains(input, pattern)) {
				MatchResult m = matcher.getMatch();
				String name = m.group(1);
				String value = m.group(2);
				//System.out.println(name);
				sb.append(name+":"+value);
				sb.append("<br/><br/>");
			}

			summary = PageProcessorUtils.getValue(SUMMARY_REG1, plotsPage);
			if (StringUtils.isNotEmpty(summary)) {
				String name = PageProcessorUtils.getValue(NAME_REG, plotsPage);
				String enName = PageProcessorUtils.getValue(EN_NAME_REG, plotsPage);
				String year = PageProcessorUtils.getValue(YEAR_REG, plotsPage);
				String logo = PageProcessorUtils.getValue(LOGO_REG, plotsPage);
				String channelCode = PageProcessorUtils.getValue(CHANNEL_CODE_REG, plotsPage);
				if ("tv".equalsIgnoreCase(channelCode)) {
					channelCode = "varietyshow";
				}
				String behindTheScenes = PageProcessorUtils.getValue(SUMMARY_REG2, plotsPage);
				String feature = PageProcessorUtils.getValue(SUMMARY_REG3, plotsPage);
				String filmReview = PageProcessorUtils.getValue(SUMMARY_REG4, plotsPage);
				
				
				baike.setName(name);
				baike.setEnName(enName);
				baike.setYear(year);
				baike.setOriLogoUrl(logo);
				baike.setChannelCode(channelCode);
				baike.setSynopsis(summary);
				baike.setBehindTheScenes(behindTheScenes);
				baike.setFeature(feature);
				baike.setFilmReview(filmReview);
				baike.setUpdateDate(new Date());
			}
		} catch ( Exception e ) {
			e.printStackTrace();
			summary="";
		}
	}
	
	private boolean canProcess(CrawlURI crawlURI) {
		int crawlStatus = crawlURI.getCrawlStatus();
		if (crawlStatus == CrawlStatusCodes.FETCH_SUCCESS) {
			return true;
		} else if (crawlStatus == CrawlStatusCodes.FETCH_SC_NOT_OK) {
			crawlURI.setCrawlStatus(CrawlStatusCodes.PAGE_PROCESS_INVALID);
			return false;
		} else {
			return false;
		}
	}
}
